package com.ykq.docter.service.impl;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ykq.core.entity.Scheduling;
import com.ykq.core.entity.User;
import com.ykq.core.vo.R;
import com.ykq.docter.feign.UserFeign;
import com.ykq.docter.mapper.SchedulingMapper;
import com.ykq.docter.service.SchedulingService;
import com.ykq.docter.vo.SchedulingDto;
import com.ykq.docter.vo.SchedulingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: 闫克起
 * @Description:
 * @Date: Create in 14:55 2024/8/21
 */
@Service
public class SchedulingServiceImpl implements SchedulingService {
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private SchedulingMapper schedulingMapper;
    @Override
    public R<Map<String, Object>> selectScheduling(SchedulingVo schedulingVo) {
        //根据部门id以及员工id查询值班医生的信息。
        Map<String,Object> params=new HashMap<>();
        params.put("userId",schedulingVo.getUserId());
        params.put("deptId",schedulingVo.getDeptId());
        //远程调用其他微服务的接口
        R<List<User>> schedulingUser = userFeign.findSchedulingUser(params);
        List<User> users = schedulingUser.getData();
        return findResult(users,schedulingVo);
    }

    private R<Map<String, Object>> findResult(List<User> users, SchedulingVo schedulingVo) {
        //创建一个当前时间。
        Date date=new Date();
        if(schedulingVo.getQueryDate()!=null){
             //判断你传入是周一还是周日
            int i = DateUtil.dayOfWeek(DateUtil.parse(schedulingVo.getQueryDate(),"yyyy-MM-dd"));
            if(i==1){//表示周日
                date=DateUtil.offsetDay(DateUtil.parse(schedulingVo.getQueryDate(),"yyyy-MM-dd"),1);
            }else{
                date=DateUtil.offsetDay(DateUtil.parse(schedulingVo.getQueryDate(),"yyyy-MM-dd"),-1);
            }
        }

        //计算给定日期的这周开始日期和结束日期
        DateTime beginDate = DateUtil.beginOfWeek(date);
        DateTime endDate = DateUtil.endOfWeek(date);

        schedulingVo.setBeginDate(DateUtil.format(beginDate,"yyyy-MM-dd"));
        schedulingVo.setEndDate(DateUtil.format(endDate,"yyyy-MM-dd"));

        //根据vo查询值班表.
       List<Scheduling> schedulings=selectSchedulingByParams(schedulingVo);
       //创建一个集合DTO对象
        List<SchedulingDto> schedulingDtos=new ArrayList<>();
        for(User user:users){
            Long userId = user.getUserId();
            //表示上午值班
            SchedulingDto schedulingDto1=new SchedulingDto(userId,user.getDeptId(),"1",initMap(beginDate));
            //表示下午值班
            SchedulingDto schedulingDto2=new SchedulingDto(userId,user.getDeptId(),"2",initMap(beginDate));
            //表示晚上值班
            SchedulingDto schedulingDto3=new SchedulingDto(userId,user.getDeptId(),"3",initMap(beginDate));

            for (Scheduling scheduling:schedulings){
                //值班类型 门诊 急诊
                String schedulingType = scheduling.getSchedulingType();
                //值班时间段 上午 下午 晚上
                String subsectionType = scheduling.getSubsectionType();
                //值班日期
                String schedulingDay = scheduling.getSchedulingDay();
                //值班医生的id
                Integer userId1 = scheduling.getUserId();
                if((userId+"").equals(userId1+"")){
                    switch (subsectionType){
                        case "1":
                            schedulingDto1.getRecord().put(schedulingDay,schedulingType);
                            break;
                        case "2":
                            schedulingDto2.getRecord().put(schedulingDay,schedulingType);
                            break;
                        case "3":
                            schedulingDto3.getRecord().put(schedulingDay,schedulingType);
                            break;

                    }
                }
            }
            schedulingDto1.setSchedulingType(schedulingDto1.getRecord().values());
            schedulingDto2.setSchedulingType(schedulingDto2.getRecord().values());
            schedulingDto3.setSchedulingType(schedulingDto3.getRecord().values());
            schedulingDtos.add(schedulingDto1);
            schedulingDtos.add(schedulingDto2);
            schedulingDtos.add(schedulingDto3);

        }

        Map<String,Object> result=new HashMap<>();
        result.put("schedulingDtos",schedulingDtos);
        result.put("startTimeThisWeek",DateUtil.format(beginDate,"yyyy-MM-dd"));
        result.put("endTimeThisWeek",DateUtil.format(endDate,"yyyy-MM-dd"));
        result.put("labelNames",getLables(beginDate));
        return R.ok(result);
    }

    //得到日期表头
    private List<String> getLables(DateTime beginDate) {
        List<String> labelNames=new ArrayList<>();
        for(int i=0;i<7;i++){
            DateTime dateTime = DateUtil.offsetDay(beginDate, i);
            String week=DateUtil.format(dateTime,"yyyy-MM-dd")+getWeek(i);
            labelNames.add(week);
        }
        return labelNames;
    }

    private String getWeek(int i) {
        String week="";
        switch (i){
            case 0:week="(周一)";break;
            case 1:week="(周二)";break;
            case 2:week="(周三)";break;
            case 3:week="(周四)";break;
            case 4:week="(周五)";break;
            case 5:week="(周六)";break;
            case 6:week="(周日)";break;
        }
        return week;
    }

    //设置一周的上午 下午 晚上的默认值班
    private Map<String, String> initMap(DateTime beginDate) {
        Map<String,String> map=new LinkedHashMap<>(); //和你添加的顺序有关
        for(int i=0;i<7;i++){
            DateTime dateTime = DateUtil.offsetDay(beginDate, i);
            map.put(DateUtil.format(dateTime,"yyyy-MM-dd"),"");
        }
        return map;
    }


    private List<Scheduling> selectSchedulingByParams(SchedulingVo schedulingVo) {
        QueryWrapper<Scheduling> wrapper=new QueryWrapper<>();
        if(Objects.nonNull(schedulingVo.getUserId())){
            wrapper.eq("user_id",schedulingVo.getUserId());
        }
        if(Objects.nonNull(schedulingVo.getDeptId())){
            wrapper.eq("dept_id",schedulingVo.getDeptId());
        }
        if(Objects.nonNull(schedulingVo.getBeginDate())){
            wrapper.between("scheduling_day",schedulingVo.getBeginDate(),schedulingVo.getEndDate());
        }
        return   schedulingMapper.selectList(wrapper);
    }

    public static void main(String[] args) {
        Date date = new Date();
        System.out.println(DateUtil.beginOfWeek(date));
        System.out.println(DateUtil.endOfWeek(date));

    }
}








